package com.hmall.gateWay.filter;

import lombok.Data;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

//局部过滤器的名字必须要以GatewayFilterFactory为类的后面部分的名
@Component
public class PrintAnyGatewayFilterFactory extends AbstractGatewayFilterFactory<PrintAnyGatewayFilterFactory.Config>{
    @Override
    public GatewayFilter apply(Config config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

                //1、获取请求
                ServerHttpRequest request = exchange.getRequest();
                //2、获取配置的参数值
                String a = config.getA();
                System.out.println(" a = " + a);
                String b = config.getB();
                System.out.println(" b = " + b);
                String c = config.getC();
                System.out.println(" c = " + c);
                //3、处理业务逻辑
                System.out.println("PrintAnyGatewayFilterFactory#filter pre被执行了。。。。。");
                //4、放行
                return chain.filter(exchange);
            }
        };
    }
    @Data
    public static class Config{
        private String a;
        private String b;
        private String c;
    }

    @Override
    public List<String> shortcutFieldOrder() {
        //指定配置文件中的参数对应的类变量顺序
        return List.of("a","b","c");
    }

    public PrintAnyGatewayFilterFactory(){
        super(Config.class); //读取配置文件中变量值
    }
}